﻿using BluetoothMachine.GlobalStatics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Prism.Commands;
using MaterialDesignThemes.Wpf;
using BluetoothMachine.Views.DialogHosts;
using System.Collections.ObjectModel;
using BluetoothMachine.Models;
using System.Text.Json;
using System.Windows;

namespace BluetoothMachine.ViewModels
{
    public class ParameterConfigurationViewModel
    {
        public ParameterConfigurationGlobal ParameterGlobal { get; set; }
        public ParameterConfigurationViewModel(ParameterConfigurationGlobal parameter)
        {
            ParameterGlobal = parameter;
            LibraryList = new ObservableCollection<string>();
        }
        private bool _checked;

        public bool Checked
        {
            get { return _checked; }
            set { 
                _checked = value;
                foreach (var item in ParameterGlobal.FilePath)
                {
                    item.WhetherEnable = value;
                }
            }
        }

        private string _productModel;

        public string ProductModel
        {
            get { return _productModel; }
            set { _productModel = value;
                ParameterGlobal.ParameterModel.ProductModel = _productModel;
                ParameterGlobal?.SelectRCCItems(-1, 0);
                ParameterGlobal?.SelectPPItems(-1, 0);
            }
        }


        private string _infraredContrastLibrary;
        /// <summary>
        /// 红外对比库
        /// </summary>
        public string InfraredContrastLibrary
        {
            get { return _infraredContrastLibrary; }
            set {
                if (value != null && _infraredContrastLibrary != value)
                {
                    ParameterGlobal.InfraredList.Clear();
                    _infraredContrastLibrary = value;
                    List<InfraredModel> list = JsonSerializer.Deserialize<List<InfraredModel>>($"{FixedParameter.InfraredPath}{value}.txt");
                    ParameterGlobal.InfraredList.AddRange(list);
                }
            }
        }

        public ObservableCollection<string> LibraryList { get; set; }

        public DelegateCommand SaveConfig => new DelegateCommand(() =>
        {
            ParameterGlobal.Save();
            MessageBox.Show("保存完成！");
            //_ = DialogHost.Show(new LoadingDialog(), "RootDialog").ConfigureAwait(false);
            //DialogHost.CloseDialogCommand.Execute(new object(), null);
        });

        public DelegateCommand<int?> SelectPath => new DelegateCommand<int?>(conut =>
        {
            Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
            openFileDialog.Title = "选择文件夹";
            //openFileDialog.Filter = "所有文件(*.*)|*.*";
            openFileDialog.FileName = "_";
            openFileDialog.FilterIndex = 1;
            openFileDialog.ValidateNames = false;
            openFileDialog.CheckFileExists = true;
            openFileDialog.CheckPathExists = true;
            openFileDialog.Multiselect = false;//允许同时选择多个文件
            //MessageBox.Show($"测试 {conut}");
            if (openFileDialog.ShowDialog() == true) ParameterGlobal.FilePath[10 - (int)conut].FilePath = $"{openFileDialog.FileName.Remove(openFileDialog.FileName.Length - 2)}\\";
        });
    }
}
